package com.zyz.N_反射机制.基本应用.调用运行时类的指定结构.练习;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author: vick
 * @Date: 2025/4/14
 * @Description:
 * @version: 1.0
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Properties properties = new Properties();
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("user.properties");
        try (InputStreamReader reader = new InputStreamReader(resourceAsStream)) {
            properties.load(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");

        //获取Class对象
        Class<?> aClass = Class.forName(className);

        //创建实例
        Object object = aClass.newInstance();

        //获取指定方法
        Method declaredMethod = aClass.getDeclaredMethod(methodName);
        //调用方法
        declaredMethod.invoke(object);

    }
}
